// ----------------------------------
// RSDK Project: Sonic 2
// Script Description: V Push Spring Object
// Script Author: Christian Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

// ========================
// Aliases
// ========================

private alias object.propertyValue : object.hasBall

private alias object.value1 : object.pushOffset
private alias object.value2 : object.collisionOffset.y
private alias object.value3 : object.beingPushed

// States
private alias 0 : VPUSHSPRING_AWAITPUSH
private alias 1 : VPUSHSPRING_PUSHING
private alias 2 : VPUSHSPRING_RELEASE
private alias 3 : VPUSHSPRING_HASBALL

// Player Aliases
private alias object.state : player.state
private alias object.ypos : player.ypos
private alias object.yvel : player.yvel
private alias object.gravity : player.gravity
private alias object.animation : player.animation

private alias object.value1  : player.timer
private alias object.value17 : debugMode.currentSelection
private alias object.value25 : player.gravityStrength

// PushButton Aliases
private alias object.value0 : pushButton.pressed


// ========================
// Function Declarations
// ========================

reserve function VPushSpring_DebugDraw
reserve function VPushSpring_DebugSpawn


// ========================
// Static Values
// ========================

private value VPushSpring_startDebugID = 0


// ========================
// Function Definitions
// ========================

private function VPushSpring_DebugDraw
	temp0 = debugMode[0].currentSelection
	temp0 -= VPushSpring_startDebugID

	DrawSprite(0)
	if temp0 == 1
		temp0 = object.ypos
		temp0 -= 0x240000
		DrawSpriteXY(5, object.xpos, temp0)
	end if
end function


private function VPushSpring_DebugSpawn
	temp0 = debugMode[0].currentSelection
	temp0 -= VPushSpring_startDebugID
	CreateTempObject(TypeName[V Push Spring], 0, object.xpos, object.ypos)
	
	object[tempObjectPos].xpos &= 0xFFFF0000
	object[tempObjectPos].ypos &= 0xFFFF0000
	if temp0 == 1
		object[tempObjectPos].state = VPUSHSPRING_HASBALL
		object[tempObjectPos].pushOffset = 0x120000
	end if
end function


// ========================
// Events
// ========================

event ObjectUpdate
	temp0 = object.pushOffset
	temp0 >>= 16
	temp0 -= 20
	switch object.state
	case VPUSHSPRING_AWAITPUSH
		object.collisionOffset.y = 0
		if object.beingPushed == true
			object.state = VPUSHSPRING_PUSHING
		end if
		break

	case VPUSHSPRING_PUSHING
		if object.pushOffset < 0x120000
			object.pushOffset += 0x20000
			object.collisionOffset.y = 0x20000
		else
			object.state = VPUSHSPRING_RELEASE
			object.collisionOffset.y = 0
		end if
		break

	case VPUSHSPRING_RELEASE
		if object.pushOffset > 0
			object.pushOffset -= 0x20000
			object.collisionOffset.y = -0x20000
		else
			object.state = VPUSHSPRING_AWAITPUSH
			object.collisionOffset.y = 0
		end if
		break

	case VPUSHSPRING_HASBALL
		if pushButton[+1].pressed == true
			if object.hasBall == false
				object.state = VPUSHSPRING_RELEASE
			end if

			PlaySfx(SfxName[Spring], false)
			temp1 = object.pushOffset
			temp1 += object.ypos
			temp1 -= 0x240000
			CreateTempObject(TypeName[Checkered Ball], 0, object.xpos, temp1)
			object[tempObjectPos].yvel = -0x60000
			object[tempObjectPos].gravity = GRAVITY_AIR
		end if
		break
		
	end switch

	object.beingPushed = false
	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		BoxCollisionTest(C_SOLID, object.entityPos, -16, temp0, 16, 20, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
		if checkResult == COL_TOP
			object.beingPushed |= true
			player[currentPlayer].ypos += object.collisionOffset.y
			if object.state == VPUSHSPRING_RELEASE
				if object.pushOffset == 0x120000
					player[currentPlayer].state = Player_State_Air
					player[currentPlayer].animation = ANI_BOUNCING
					player[currentPlayer].gravity = GRAVITY_AIR
					player[currentPlayer].yvel = -0xA0000
					player[currentPlayer].yvel -= player[currentPlayer].gravityStrength
					player[currentPlayer].timer = 0
					PlaySfx(SfxName[Spring], false)
				end if
			end if
		end if
	next
end event


event ObjectDraw
	if object.pushOffset == 0
		DrawSprite(0)

		temp0 = object.ypos
		temp0 -= 0x240000
		if object.state == VPUSHSPRING_HASBALL
			temp0 -= 16 // 16? That's it? This is a 16.16 fixed point position, just "16" alone won't do much...
			DrawSpriteXY(5, object.xpos, temp0)
		end if
	else
		temp0 = object.pushOffset
		temp0 += object.ypos
		DrawSpriteXY(1, object.xpos, temp0)

		if object.state == VPUSHSPRING_HASBALL
			temp0 -= 0x240000
			DrawSpriteXY(5, object.xpos, temp0)

			object.inkEffect = INK_ADD
			object.alpha 	 = 0xA0
			DrawSpriteFX(6, FX_INK, object.xpos, temp0)
		end if

		temp0 = object.pushOffset
		temp0 *= 2
		temp0 /= 3
		temp0 += object.ypos
		DrawSpriteXY(2, object.xpos, temp0)

		temp0 = object.pushOffset
		temp0 /= 3
		temp0 += object.ypos
		DrawSpriteXY(3, object.xpos, temp0)

		DrawSprite(4)
	end if
end event


event ObjectStartup
	LoadSpriteSheet("OOZ/Objects.gif")
	SpriteFrame(-16, -20, 32, 40, 239, 140)
	SpriteFrame(-16, -20, 32, 16, 239, 140)
	SpriteFrame(-16, -4, 32, 8, 239, 156)
	SpriteFrame(-16, 4, 32, 8, 239, 164)
	SpriteFrame(-16, 12, 32, 8, 239, 172)
	SpriteFrame(-16, -16, 32, 32, 429, 190)
	SpriteFrame(-16, -16, 32, 32, 396, 190)

	temp0 = 0
	VPushSpring_startDebugID = DebugMode_ObjCount
	while temp0 < 2
		SetTableValue(TypeName[V Push Spring], DebugMode_ObjCount, DebugMode_TypesTable)
		SetTableValue(VPushSpring_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
		SetTableValue(VPushSpring_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
		DebugMode_ObjCount++
		temp0++
	loop

	foreach (TypeName[V Push Spring], arrayPos0, ALL_ENTITIES)
		if object[arrayPos0].hasBall == true
			object[arrayPos0].state = VPUSHSPRING_HASBALL
			object[arrayPos0].pushOffset = 0x120000
		end if
	next
end event


// ========================
// Editor Events
// ========================

event RSDKEdit
	if editor.returnVariable == true
		switch editor.variableID
		case EDIT_VAR_PROPVAL // property value
			checkResult = object.propertyValue
			break
			
		case 0 // hasBall
			checkResult = object.propertyValue
			checkResult &= 1
			break
			
		end switch
	else
		switch editor.variableID
		case EDIT_VAR_PROPVAL // property value
			object.propertyValue = editor.variableValue
			break
			
		case 0 // hasBall
			editor.variableValue &= 1
			object.propertyValue = editor.variableValue
			break
			
		end switch
	end if
end event


event RSDKDraw
	DrawSprite(0)

	temp0 = object.ypos
	temp0 -= 0x240000
	if object.hasBall == true
		DrawSpriteXY(5, object.xpos, temp0)
	end if
end event


event RSDKLoad
	LoadSpriteSheet("OOZ/Objects.gif")
	SpriteFrame(-16, -20, 32, 40, 239, 140)
	SpriteFrame(-16, -20, 32, 16, 239, 140)
	SpriteFrame(-16, -4, 32, 8, 239, 156)
	SpriteFrame(-16, 4, 32, 8, 239, 164)
	SpriteFrame(-16, 12, 32, 8, 239, 172)
	SpriteFrame(-16, -16, 32, 32, 429, 190)
	SpriteFrame(-16, -16, 32, 32, 396, 190)

	AddEditorVariable("hasBall")
	SetActiveVariable("hasBall")
	AddEnumVariable("false", false)
	AddEnumVariable("true", true)
end event
